package com.hopu.blb_erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hopu.blb_erp.entity.Order2;
import com.hopu.blb_erp.entity.PageEntity;
import com.hopu.blb_erp.entity.ProductHandover;
import com.hopu.blb_erp.mapper.Order2Mapper;
import com.hopu.blb_erp.mapper.ProductHandoverMapper;
import com.hopu.blb_erp.service.IProductHandoverService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author A-Zhang
 * @since 2021-11-16
 */
@Service
public class ProductHandoverServiceImpl extends ServiceImpl<ProductHandoverMapper, ProductHandover> implements IProductHandoverService {
    @Autowired
    private ProductHandoverMapper productHandoverMapper;
    @Autowired
    private Order2Mapper order2Mapper;
    @Override
    public PageEntity<ProductHandover> findAllProductHandoverPage(int current, int PAGE_SIZE, int orderId,LocalDate handoverDate) {
        int count = Math.toIntExact(productHandoverMapper.selectCount(null));
        int pageCount =  count % PAGE_SIZE==0?count/PAGE_SIZE:count/PAGE_SIZE+1;
        List<ProductHandover> productHandover = productHandoverMapper.selectAllProductHandoverLimit((current-1)*PAGE_SIZE,PAGE_SIZE,orderId,handoverDate);
        PageEntity<ProductHandover> page = new PageEntity<>();
        page.setCurrent(current);
        page.setData(productHandover);
        page.setPageCount(pageCount);
        page.setCount(count);
        return page;
    }

    /**
     * 在添加一个订单之后，将订单的生产状态从3（已生产）改为4（待入库）
     * @param productHandover
     */
    @Transactional
    @Override
    public void addProductHandover(ProductHandover productHandover,Integer order2Id) {
        productHandoverMapper.insert(productHandover);
//        LambdaUpdateWrapper<Order2> luw = Wrappers.lambdaUpdate();
//        luw.set(Order2::getProductStatus,4);
//        luw.eq(Order2::getId,order2Id);
//        order2Mapper.update(null,luw);
        order2Mapper.update(null,Wrappers.<Order2>lambdaUpdate().set(Order2::getProductStatus,4).eq(Order2::getId,order2Id));
    }

    /**
     * 在更新交接单状态之后，对订单的status进行修改
     */
    @Transactional
    @Override
    public void updateProductHandover(Integer id,Integer order2Id) {
        ProductHandover productHandover = productHandoverMapper.selectById(id);
        if(productHandover.getState() == 1){
            productHandoverMapper.update(null, Wrappers.<ProductHandover>lambdaUpdate().set(ProductHandover::getState,0).eq(ProductHandover::getId,id));
            order2Mapper.update(null,Wrappers.<Order2>lambdaUpdate().set(Order2::getProductStatus,4).eq(Order2::getOrderId,order2Id));
        }else{
            productHandoverMapper.update(null, Wrappers.<ProductHandover>lambdaUpdate().set(ProductHandover::getState,1).eq(ProductHandover::getId,id));
            order2Mapper.update(null,Wrappers.<Order2>lambdaUpdate().set(Order2::getProductStatus,5).eq(Order2::getOrderId,order2Id));
        }
    }

    @Override
    public List<ProductHandover> findAllProductHandover() {
        return productHandoverMapper.selectAllProductHandover();
    }


}
